<template>
  <div>
    <el-card class="box-construction" shadow="always">
      <div class="box-title box-title-position">
        <div>项目类别</div>
        <el-button class="add-btn" size="medium" type="primary" @click="addClassic()">新增类别</el-button>
      </div>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        style="width: 100%;margin-top:25px"
      >
        <el-table-column :show-overflow-tooltip="true" prop="id" label="ID" width="50"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="name" label="名称"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="status" label="状态" width="130">
          <template slot-scope="scope">
            <span>{{scope.row.status=="0"?"禁用":"启用"}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="version" label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="small" icon="el-icon-edit" @click="editClassic(scope.row)">编辑</el-button>
            <el-button size="small" icon="el-icon-delete" @click="delClsaaic(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <ClassicForm
      ref="editForm"
      :title="formTitle"
      :datas.sync="editRow"
      :visible.sync="visible"
      :baseStyle="baseStyle"
      @saveForm="submit"
    />
  </div>
</template>

<script>
import classicAPI from "@apis/projectClassic";
import ClassicForm from "./editClassicForm/index";
export default {
  data() {
    return {
      loading: false,
      tableData: [],
      editRow: {},
      visible: false,
      baseStyle: {
        width: "140px"
      },
      formTitle: "新增类别"
    };
  },
  components: {
    ClassicForm
  },
  created() {
    this.loading = true;
    this.getList();
  },
  mounted() {},
  methods: {
    getList() {
      classicAPI
        .getList()
        .then(res => {
          this.tableData = [...res.list];
          this.loading = false;
        })
        .catch(err => {
          this.loading = false;
          this.$message.error(err.message);
        });
    },
    addClassic() {
      this.formTitle = "新增项目类别";
      this.visible = true;
      this.editRow = { status: 1 };
    },
    editClassic(row) {
      this.formTitle = "编辑项目类别";
      this.editRow = { ...row };
      this.visible = true;
    },
    delClsaaic(data) {
      this.$confirm("删除类别, 是否继续?", "提示", {
        closeOnClickModal: false,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.loading = true;
          classicAPI
            .classDel(data.id)
            .then(res => {
              this.getList();
            })
            .catch(err => {
              this.$message.error(err.message);
            });
        })
        .catch(() => {});
    },
    submit(val) {
      classicAPI
        .classAddEdit(val)
        .then(res => {
          this.$refs.editForm.cancel();
          this.loading = true;
          this.getList();
        })
        .catch(err => this.$message.error(err.message));
    }
  }
};
</script>

<style lang="scss" scoped>
.box-title-position {
  position: relative;
  .add-btn {
    position: absolute;
    right: 0;
    top: -3px;
  }
}
.box-construction {
  min-height: 100px;
  .box-page {
    text-align: right;
    padding-top: 40px;
  }
}
</style>